//
//  ONBaseViewController.m
//  HairCutSalon
//
//  Created by Familly on 5/27/14.
//  Copyright (c) 2014 Familly. All rights reserved.
//

#import "ONBaseViewController.h"
#import "ONLoginViewController.h"

@interface ONBaseViewController () <EGORefreshTableHeaderDelegate>

@end

@implementation ONBaseViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
//    self.rootViewController=(ONRootViewController *)APP_DELEGATE.window.rootViewController;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7){
        //give cordinates here
        [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleLightContent];
     }
    [self updateUIForTabbar];
    // Set title for controller
    self.tabBarController.title = @"Volatix Power Market";
}
-(NSString *)getRequestFiter:(int)typeScreen
{
    NSString *getLoadZone =[Utils getLoadZone];
    NSString *getISO =[Utils getISO];
    NSLog(@"%@ - %@ ",getISO,getLoadZone);
    
    NSString *temp =(typeScreen==0)?@"FILTER_REAL_TIMER":(typeScreen==1)?@"FILTER_DAY_AHEAD":@"FILTER_ALERT";
    NSString *ISO =(getISO==nil)?@"":getISO;
    NSString *LoadZone =(getISO==nil)?@"":getLoadZone;
    
    NSString *request=[NSString stringWithFormat:@"%@%@%@",ISO,LoadZone,temp];
    
    return request;
}
- (void)updateUIForTabbar {
    // Get TabbarItem from Tabbar
    UITabBar *tabBar = self.tabBarController.tabBar;
    UITabBarItem *tabBarItem0 = [tabBar.items objectAtIndex:0];
    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:1];
    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:2];
    UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:3];
    
    // Set Icon for Tabbar Item
    [tabBarItem0 setFinishedSelectedImage:[UIImage imageNamed:@"btn_tab_RealTime_active"] withFinishedUnselectedImage:[UIImage imageNamed:@"btn_tab_RealTime"]];
    [tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"btn_tab_DayHead_active"] withFinishedUnselectedImage:[UIImage imageNamed:@"btn_tab_DayHead"]];
    [tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"btn_tab_Alert_active"] withFinishedUnselectedImage:[UIImage imageNamed:@"btn_tab_Alert"]];
        [tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"btn_tab_Setting_active"] withFinishedUnselectedImage:[UIImage imageNamed:@"btn_tab_Setting"]];
}

-(IBAction)backToLeftMenu
{
    [self.view endEditing:YES];
    //[self.rootViewController showLeftPanelAnimated:YES];
}

- (UITableViewCell *)getLoadingCellForTableView:(UITableView *)tableView {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    
    // Create a cell with active indicator
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        
        // Add loading indicator
        UIActivityIndicatorView *loadingIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
        loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
        loadingIndicator.tag = 2000;
        loadingIndicator.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [cell.contentView addSubview:loadingIndicator];
    }
    
    // Start loadingIndicator
    UIActivityIndicatorView *loadingIndicator = (UIActivityIndicatorView *)[cell viewWithTag:2000];
    [loadingIndicator startAnimating];
    return cell;
}

- (void)startPullDownRefresh {
    // Add Pull Down Refresh view
    if (self._refreshHeaderView == nil) {
		
		EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.tableView.bounds.size.height, self.view.frame.size.width, self.tableView.bounds.size.height)];
		view.delegate = self;
		[self.tableView addSubview:view];
		self._refreshHeaderView = view;
	}
    
    //  update the last update date
    [self._refreshHeaderView refreshLastUpdatedDate];
    
    // manual trigger loading
//    [self._refreshHeaderView egoRefreshScrollViewDataSourceStartManualLoading:self.tableView];
}

- (BOOL)userIsLogin {
    // Get accesstoken
    NSString *accessToken = [[NSUserDefaults standardUserDefaults]objectForKey:kAccessToken];
    if ([accessToken length] > 0) {
        return YES;
    }
    return NO;
}

- (void)saveAccessToken:(NSString *)accessToken {
    // Save token
    [[NSUserDefaults standardUserDefaults]setObject:accessToken forKey:kAccessToken];
}

- (void)logoutUser {
    [self saveAccessToken:nil];
}

- (void)showLoginScreen {
    ONLoginViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"ONLoginViewController"];
    [self presentViewController:controller animated:YES completion:nil];
}

#pragma mark - UIScrollViewDelegate Methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
	
	[self._refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    // When table is not searching.
    [self._refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
}

#pragma mark - EGORefreshTableHeaderDelegate Methods

- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view {
   [self requestGetData];
    NSLog(@"request AAAA");
}

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view {
	
	return _reloading; // should return if data source model is reloading
}

- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view {
	
	return [NSDate date]; // should return date data source was last changed
}

#pragma mark - Data Source Loading / Reloading Methods

- (void)reloadTableViewDataSource {
	
	//  should be calling your tableviews data source model to reload
	//  put here just for demo
	_reloading = YES;
}
-(void)requestGetData
{
    
}
- (void)doneLoadingTableViewData {
	
	//  model should call this when its done loading
	_reloading = NO;
	[self._refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
}

- (void)dealloc {
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
}
@end
